package l2ft.gameserver.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Collection;

import l2ft.gameserver.database.DatabaseFactory;
import l2ft.gameserver.model.entity.events.impl.DominionSiegeEvent;
import l2ft.gameserver.model.entity.residence.Dominion;

import org.apache.log4j.Logger;
import org.napile.primitive.maps.IntObjectMap;

public class DominionRewardDAO
{
	private static final String INSERT_SQL_QUERY = "INSERT INTO dominion_rewards (id, object_id, static_badges, online_reward, kill_reward) VALUES (?,?,?,?,?)";
	private static final String SELECT_SQL_QUERY = "SELECT * FROM dominion_rewards WHERE id=?";
	private static final String DELETE_SQL_QUERY = "DELETE FROM dominion_rewards WHERE id=? AND object_id=?";
	private static final String DELETE_SQL_QUERY2 = "DELETE FROM dominion_rewards WHERE id=?";

	private static final Logger _log = Logger.getLogger(DominionRewardDAO.class);
	private static final DominionRewardDAO _instance = new DominionRewardDAO();

	public static DominionRewardDAO getInstance()
	{
		return _instance;
	}

	public void select(Dominion d)
	{
		DominionSiegeEvent siegeEvent = d.getSiegeEvent();

		try(Connection con = DatabaseFactory.getConnection();
				PreparedStatement statement = con.prepareStatement(SELECT_SQL_QUERY))
		{
			statement.setInt(1, d.getId());
			
			try(ResultSet rset = statement.executeQuery())
			{
				while(rset.next())
				{
					int playerObjectId = rset.getInt("object_id");
					int staticBadges = rset.getInt("static_badges");
					int onlineReward = rset.getInt("online_reward");
					int killReward = rset.getInt("kill_reward");
	
					siegeEvent.setReward(playerObjectId, DominionSiegeEvent.STATIC_BADGES, staticBadges);
					siegeEvent.setReward(playerObjectId, DominionSiegeEvent.KILL_REWARD, killReward);
					siegeEvent.setReward(playerObjectId, DominionSiegeEvent.ONLINE_REWARD, onlineReward);
				}
			}
		}
		catch(Exception e)
		{
			_log.error("DominionRewardDAO:select(Dominion): " + e, e);
		}
	}

	public void insert(Dominion d)
	{
		try(Connection con = DatabaseFactory.getConnection())
		{
			try(PreparedStatement statement = con.prepareStatement(DELETE_SQL_QUERY2))
			{
				statement.setInt(1, d.getId());
				statement.execute();
			}

			DominionSiegeEvent siegeEvent = d.getSiegeEvent();
			Collection<IntObjectMap.Entry<int[]>> rewards = siegeEvent.getRewards();

			try(PreparedStatement statement = con.prepareStatement(INSERT_SQL_QUERY))
			{
				for(IntObjectMap.Entry<int[]> entry : rewards)
				{
					statement.setInt(1, d.getId());
					statement.setInt(2, entry.getKey());
					statement.setInt(3, entry.getValue()[DominionSiegeEvent.STATIC_BADGES]);
					statement.setInt(4, entry.getValue()[DominionSiegeEvent.ONLINE_REWARD]);
					statement.setInt(5, entry.getValue()[DominionSiegeEvent.KILL_REWARD]);
					statement.addBatch();
				}
	
				if(!rewards.isEmpty())
				{
					statement.executeBatch();
				}
			}
		}
		catch(final Exception e)
		{
			_log.error("DominionRewardDAO.insert(Dominion):", e);
		}
	}

	public void delete(Dominion d, int objectId)
	{
		try(Connection con = DatabaseFactory.getConnection();
				PreparedStatement statement = con.prepareStatement(DELETE_SQL_QUERY))
		{
			statement.setInt(1, d.getId());
			statement.setInt(2, objectId);
			statement.execute();
		}
		catch(Exception e)
		{
			_log.error("DominionRewardDAO:delete(Dominion): " + e, e);
		}
	}
}
